package com.snail.readyvr.mvp.presenter;

import com.snail.readyvr.mvp.contract.MeActivityContract;
import com.snail.readyvr.mvp.model.ChangeHeadIconParam;
import com.snail.readyvr.mvp.model.ChangeNickNameParam;
import com.snail.readyvr.mvp.model.LoginResponse;
import com.snail.readyvr.rest.HttpResultFunc;
import com.snail.readyvr.rest.LoginRestClient;
import com.snail.readyvr.rest.RestApiUtils;
import com.snail.readyvr.util.Constant;
import com.snail.readyvr.util.EventBus;
import com.snail.readyvr.util.Utils;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by MRKING on 2016/11/10.
 */

public class MeActivityPresenter implements MeActivityContract.Presenter {
    private MeActivityContract.View mView;
    private LoginRestClient mRestClient;

    @Inject
    public MeActivityPresenter(MeActivityContract.View view, LoginRestClient restClient) {
        mView = view;
        mRestClient = restClient;
    }


    @Override
    public void start() {

    }

    @Override
    public void changeNickName(String token, final ChangeNickNameParam param) {

        mRestClient.changeNickName(token, param).map(new HttpResultFunc<LoginResponse>(RestApiUtils.CHNEG_NICK_NAME))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        if (mView != null && Utils.myAccountInfo != null) {
                            Utils.myAccountInfo.setNickname(param.getNickname());
                            mView.changeNickNameSuccess(param.getNickname());
                            EventBus.getDefault().post(Constant.BusTag.CHANGE_NICKNAME_TAG, param);
                        }

                    }
                });
    }

    @Override
    public void changeHeadIcon(String token,final ChangeHeadIconParam param) {
        mRestClient.changeHeadIcon(token, param).map(new HttpResultFunc<LoginResponse>(RestApiUtils.CHANGE_HEAD_ICON))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        if (mView != null && Utils.myAccountInfo != null) {
                            Utils.myAccountInfo.setHead_icon(param.getHead_icon());
                            mView.changeHeadIconSuccess(param.getHead_icon());
                            EventBus.getDefault().post(Constant.BusTag.CHANGE_HEADICON_TAG, param);
                        }
                    }
                });
    }
}
